<?php

/**
 * Created by PhpStorm.
 * User: qiliping
 * Date: 2016/12/1
 * Time: 上午9:09
 */
class Login extends MY_Controller
{
    /**
     * 登录
     * @return [type] [description]
     */
    // public function index(){
    //     $post_data = $this->input->post();
    //     if($post_data){
    //         if(empty($post_data['mobile'])){
    //             echo json_encode("请输入手机号码！");exit;
    //         }
    //         if(empty($post_data['pwd'])){
    //             echo json_encode("请输入密码");exit;
    //         }
    //         //判断手机号是否存在
    //         $this->load->model('User_model');
    //         $user = $this->User_model->get_user_by_phone($post_data['mobile']);
    //         if(empty($user)){
    //              echo json_encode("手机帐号不存在！");exit;
    //         }else{
    //           $password = md5(md5($post_data['pwd']) . $user['rand_str']);
    //
    //             if($user['password']!=$password){
    //                 echo json_encode("密码错误！");exit;
    //             }else{
    //                 //设置cookie
    //                 $user_data = array(
    //                     'id' => $user['id'],
    //                     'head_image' => $user['head_image_url'],
    //                     'user_name' => $user['login_name'],
    //                     'mobile' => $user['mobile'],
    //                     'logged_in' => 1);
    //                 $this->session->set_userdata('user_data', $user_data);
    //                 $curr_url =  $this->session->userdata('curr_url');
    //                 echo 1;exit;
    //                 redirect("/");
    //                 // login_wecenter($post_data['phone'], $post_data['pwd']);
    //                 // if(empty($curr_url)){
    //                 //   header("Location:/index");exit;
    //                 // }else{
    //                 //   header("Location:{$curr_url}");exit;
    //                 // }
    //
    //             }
    //         }
    //
    //     }
    //     $this->smarty->display("login.html");
    // }


     /* 退出
     * @return [type] [description]
     */
    function logout(){

      $this->session->sess_destroy();
        unset(
            $_SESSION['id'],
            $_SESSION['head_image'],
            $_SESSION['user_name'],
            $_SESSION['mobile'],
            $_SESSION['logged_in']
        );
        $curr_url =  $this->session->userdata('curr_url');
        foreach($_COOKIE as $key=>$val){
            setcookie($key, "", time() - 1, '/');
        }
        if(empty($curr_url)){
          header("Location:/login");exit;
        }else{
          header("Location:{$curr_url}");exit;
        }
    }


    public function ajax(){
        $post_data = $this->input->post();

        if($post_data){

          //  print_r($post_data);exit;
            if(empty($post_data['phone'])){
                print_r('{"result":"-1","msg":"手机号不能为空"}');exit;
            }
            if(empty($post_data['pwd'])){
                print_r('{"result":"-1","msg":"密码不能为空"}');exit;
            }

            //判断手机号是否存在
            $this->load->model('User_model');
            $user = $this->User_model->get_user_by_phone($post_data['phone']);

            if(empty($user)){
                print_r('{"result":"404","msg":"手机号不存在"}');exit;
            }else{
              $password = md5(md5($post_data['pwd']) . $user['rand_str']);

                if($user['password']!=$password){
                    print_r('{"result":"400","msg":"密码错误"}');exit;
                }else{
                    //设置cookie
                    $user_data = array(
                        'id' => $user['id'],
                        'head_image' => $user['head_image_url'],
                        'user_name' => $user['login_name'],
                        'mobile' => $user['mobile'],
                        'logged_in' => 1);
                    $this->session->set_userdata('user_data', $user_data);
                    print_r('{"result":"0","msg":"success"}');exit;


                }
            }


        }

    }

}
